package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.UpdateDataDTO;
import com.xbongbong.pro.script.pojo.dto.UpdateFundManagemenExpalinstDTO;
import com.xbongbong.pro.script.pojo.dto.UpdateTabDTO;
import com.xbongbong.pro.script.pojo.vo.ScriptVO;
import com.xbongbong.saas.service.RepairDataService;
import com.xbongbong.saas.service.RepairExplainService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020-02-12 10:49
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/purchasePayplan")
public class PurchaseAndPayPlanScriptController {
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private RepairExplainService repairExplainService;
    @Resource
    private RepairDataService repairDataService;
    
    @RequestMapping(value = "/explains", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String explains(@RequestBody @Valid UpdateFundManagemenExpalinstDTO updateFundManagemenExpalinstDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateFundManagemenExpalinstDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairExplainService.scriptParchaseAndPayPlanExplains(updateFundManagemenExpalinstDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    @RequestMapping(value = "/scriptTab", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String scriptTab(@RequestBody @Valid UpdateTabDTO updateTabDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateTabDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairExplainService.scriptPayPlanTab(updateTabDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    @RequestMapping(value = "/scriptDetailTab", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String scriptDetailTab(@RequestBody @Valid UpdateTabDTO updateTabDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateTabDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairExplainService.scriptPayPlanDetail(updateTabDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    @RequestMapping(value = "/scriptPayPlanData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String scriptPayPlanData(@RequestBody @Valid UpdateDataDTO updateDataDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateDataDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairDataService.scriptPayPlanData(updateDataDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    @RequestMapping(value = "/scriptPurchaseData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String scriptPurchaseData(@RequestBody @Valid UpdateDataDTO updateDataDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateDataDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairDataService.scriptPurchaseData(updateDataDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/payPlanFinishAmount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String payPlanFinishAmount(@RequestBody @Valid UpdateDataDTO updateDataDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateDataDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairDataService.payPlanFinishAmount(updateDataDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/purchaseFinishAmount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String purchaseFinishAmount(@RequestBody @Valid UpdateDataDTO updateDataDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if ( !proBaseConfig.getXbbCode().equals(updateDataDTO.getXbbCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ScriptVO detailTabScriptVO = repairDataService.purchaseFinishAmount(updateDataDTO);
                response = new XbbResponse<>(detailTabScriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
